<?php

namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\Request;
use think\Session;


class Auser extends AdminController
{
    public function _initialize(){
        parent::_initialize();
    }

    public function index()
    {
        // 读取用户列表
        $data =Db::table('sm_a_user')
                ->alias('sau')
                ->join('sm_a_user_role saur', 'saur.uid  = sau.id')
                ->join('sm_a_role sar', 'sar.id = saur.rid')
                ->field('sau.username,sau.name as sauname,sar.name as sarname,sau.id')
                ->order('sau.id')
                // ->paginate(10);
                ->select();
        $res = Db::name('a_user')->field(['id', 'name','username','status'])->order('id', 'ASC')->paginate(5);
        $page = $res->render();
        // $data = Db::name('a_count')->order('id','desc')->limit(7)->select();

        return view('admin@Auser/index',[
            'data' => $data,
            'title' => '显示管理员',
            'res' => $res,
            'page' => $page,
        ]);
    }

    public function add()
    {
        return view('admin@Auser/add',[
            'title' => '添加管理员',
        ]);
    }

    // 添加管理员
    public function insert(Request $request)
    {
        $data = $request->post();
        // var_dump($data);die;
        $d['UserName'] = $data['UserName'];
        $d['Name'] = $data['Name'];
        $d['Password'] = md5($data['Password']);

        $s = [
            'username' => $d['UserName'],
            'name' => $d['Name'],
            'userpass' => $d['Password']
        ];

        $result = Db('a_user')->insert($s);

        $uid = Db::name('a_user')->field('id')->where('name', $d['Name'])->find();
        $d = [
            'uid' => $uid['id'],
            'rid' => '7',
        ];
        $list = db('a_user_role')->insert($d);
        if ($result) {
            return $this->success('新增成功');
        } else {
            return $this->error('新增失败');
        }
    }

    public function delete($id)
    {
        // echo $id;
       $result = db('a_user')->delete($id);
       if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除失败!';
        }
        return json($info);
    }

    public function edit($id)
    {
        $data = db('a_user')->field('pass', true)->find($id);
        return view('admin@Auser/edit',[
            'title' => '修改ID:'.$id.'管理员',
            'data' => $data,
            'id' => $id
        ]);
    }

    public function update(Request $request)
    {
        $data = $request->post();
        $id = $data['id'];
        if ($data['Password'] == '') {
            unset($data['Passeword']);
            $a = [
            'name' => $data['Name'],
            'status' => $data['go'],
            ];
        }else{
            $data['Password'] = md5($data['Password']);
            $a = [
            'name' => $data['Name'],
            'userpass' => $data['Password'],
            'status' => $data['go'],
            ];
        }

        $result = Db::name('a_user')->where('id',$id)->update($a);

        if ($result) {
            return $this->success('编辑成功');
        } else {
            return $this->error('编辑失败');
        }
    }

    public function checkPower(Request $request)
    {
        $data = $request->post();
        $result = db('a_user_role')->find($data);

        $resource = db('a_role')->field(['id','name'])->select();
        
        foreach ($resource as $k => $v) {
            if ($v['id'] == $result['rid']) {
            // var_dump($result['uid']);
                $resource[$k]['status'] = 1;
            };
                
        }
        return json($resource);
    }

// 分配角色页面
    public function getRole($id)
    {
       
        $input = input();
        $uid = $input['id'];
        // var_dump($uid);die;
        $list = Db::table('sm_a_user')
                ->alias('sau')
                ->join('sm_a_user_role saur', 'saur.uid = sau.id')
                ->join('sm_a_role sar', 'sar.id = saur.rid')
                ->field('sau.username,sau.name as sauname,sar.name as sarname,sar.id as asrid')
                ->where('sau.id',$uid)
                ->select();

        // var_dump($list);
        $rname = $list;
        $role = Db::name('a_role')->field(['id', 'name',])->order('id', 'ASC')->select();

        foreach($rname as $v){
            $myrole[] = $v['asrid'];
        }
        
        return view('admin@Auser/chooseRole', [
            'role' => $role,
            'rname' => $myrole,
            'uid' => $uid,
            'title' => 'ID:'.$uid.'管理员分配角色',
        ]);

    }

    public function updateRole(Request $request)
    {
        $data = $request->post();
        // var_dump($data);
        $id = $data['uid'];
        // 删除原来角色
        $r = Db::name('a_user_role')->where('uid',$id)->delete();


        foreach ($data['role'] as $k => $v) {
            $a = [
                'rid' => $v,
                'uid' => $id,
            ];
            // var_dump($a);
            $result = Db::name('a_user_role')->where('uid',$id)->insert($a);
        }
        


        if ($result) {
            return $this->success('分配节点成功');
        } else {
            return $this->error('分配节点失败');
        }
    }

     function search()
{
    $where = [];
    $keyword  = isset($_GET['keyword'])?$_GET['keyword'] : '';
    if($keyword){
        $where['username|name'] = ['like', '%'.$keyword.'%'];
     
     $list = Db::name('a_user')->where($where)->order('id DESC')->select();
     $data =Db::table('sm_a_user')
                ->alias('sau')
                ->join('sm_a_user_role saur', 'saur.uid  = sau.id')
                ->join('sm_a_role sar', 'sar.id = saur.rid')
                ->field('sau.username,sau.name as sauname,sar.name as sarname,sau.id')
                ->order('sau.id')
                ->select();
    if ($list) {
        return view('admin@Auser/search',[
            'title' => '搜索管理员关键字:'.$keyword,
            'list' => $list,
            'data' => $data,
        ]);
    }else{
        return view('admin@Auser/search',[
            'title' => '搜索管理员关键字:'.$keyword,
            'res' =>'没有找到关于 ['.$keyword.'] 的管理员，换个搜索词试试吧。',
        ]);
    }
     
 }else{
    return view('admin@Auser/search',[
            'title' => '搜索管理员关键字:'.$keyword,
            'res' =>'没有找到关于 [  ] 的管理员，换个搜索词试试吧。',
        ]);
 }
}

}